<template>
	<view class="content">
		<!-- 头部 -->
		<Header ref="Header" title="本地生活服务"></Header>
		<!-- 搜索框 -->
		<view class="serch-box">
			<u-search placeholder="搜索你想要的店铺" v-model="queryForm.q" inputAlign="center" :clearabled="true"
				:showAction="false" @search="getList()" @clear="getList()">
			</u-search>
		</view>
		<!-- 筛选标签 -->
		<view class="screen">
			<u-tabs :list="categoryList" keyName="title" :activeStyle="{
			color: '#333333',
			fontWeight: 'bold',
			fontSize:'40rpx'
			}" :inactiveStyle="{
			color: '#919191',
			fontWeight: '400',
			fontSize:'32rpx'
			}" lineColor="rgba(243, 97, 95, 0.2)" @click="changCategory"></u-tabs>
		</view>
		<!-- 列表 -->
		<view class="shop-list">
			<view class="shop-item" v-for="(item,index) in dataList" :key="item.id" @click="toDetails(item)">
				<view class="left">
					<u--image :src="item.shop_images | setImgUrl" width="165rpx" height="165rpx" mode="aspectFill">
						<view slot="error" style="font-size: 24rpx;">加载失败</view>
					</u--image>
				</view>
				<view class="center">
					<!-- <view >
						
					</view> -->
					<u--text class="title" :lines="1" :text="item.shop_name"></u--text>
					<view class="type-box">
						<view class="type" v-if="item.labelList && item.labelList.length">
							<text v-for="(lableItem,elIndex) in item.labelList" :key="elIndex">{{lableItem}}</text>
						</view>
						<view class="right">
							{{item.distance}}
						</view>
					</view>
					<view class="tip" v-if="item.discount">
						<view class="icon">
							惠
						</view>
						<view>
							{{item.discount}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Header from '@/components/header.vue'
	export default {
		data() {
			return {
				title: 'Hello',
				keyword: '',
				categoryList: [{
					id: 0,
					title: '推荐'
				}],
				queryForm: {
					q: "",
					cate_id: 0,
					lng: '',
					lat: '',
					page: 1
				},
				dataList: []
			}
		},
		components: {
			Header
		},
		mounted() {

		},
		onLoad() {
			this.getLoc()
		},
		onReachBottom() {
			this.queryForm.page+=1
			this.getList()
		},

		methods: {
			// 跳转详情
			toDetails(item) {
				if (!uni.getStorageSync('token')) {
					this.$u.toast('未登录，请先登录')
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500)
					return;
				}
				uni.navigateTo({
					url: '/pages/shop/shop?id=' + item.id
				})
			},
			// 获取位置
			getLoc() {
				uni.getLocation({
					success: (res) => {
						this.queryForm.lng = res.longitude
						this.queryForm.lat = res.latitude
						this.getShopCategory()
					}
				})
			},
			// 切换分类
			changCategory(item) {
				this.dataList = []
				if (item.id == this.queryForm.cate_id) {
					return
				}
				this.queryForm.cate_id = item.id
				this.getList()
			},
			// 获取店铺列表
			getList() {
				uni.$u.http.post('/shop/shoplist', this.queryForm).then(res => {
					this.dataList = [...this.dataList, ...res.data.data]
					console.log(this.dataList)
					// this.dataList.forEach(el => {
					// 	el.labelList = el.label.split(',')
					// 	el.shopimgList = el.shop_images.split(',')
					// 	el.shop_image = el.shopimgList.length ? el.shopimgList[0] : ''
					// })

				})
			},
			// 获取店铺分类
			getShopCategory() {
				uni.$u.http.post('/shop/shopcategory', ).then(res => {
					this.categoryList = this.categoryList.concat(res.data)
					// if (res.data && res.data.length) {
					// 	this.queryForm.cate_id = res.data[0].id
					// }
					this.getList()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.serch-box {
		margin-top: 38rpx;
		width: 588rpx;
	}

	.screen {
		margin-top: 8rpx;
		width: 684rpx;
	}

	.shop-list {
		width: 684rpx;
		margin-top: 12rpx;
		padding-bottom: 30px;
	}

	.shop-item {
		// min-height: 236rpx;
		padding: 34rpx 0px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-top: 1rpx solid #d8d8d8;

		.left {
			width: 165rpx;
			height: 165rpx;
			border-radius: 24rpx;
			overflow: hidden;
		}

		.center {
			width: 490rpx;
			// height: 165rpx;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333333;
				margin-top: 12rpx;
			}

			.type-box {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;

				.type {
					width: 340rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: #333333;
					margin-top: 16rpx;

					text {
						margin-right: 20rpx;
					}
				}

				.right {
					font-size: 24rpx;
					font-weight: 400;
					color: #333333;
					margin-top: 16rpx;
				}
			}

			.tip {
				font-size: 20rpx;
				font-weight: 400;
				color: #333333;
				display: flex;
				align-items: center;
				margin-top: 34rpx;

				.icon {
					width: 26rpx;
					height: 24rpx;
					background: #FF7200;
					border-radius: 4rpx;
					line-height: 24rpx;
					text-align: center;
					color: #FFFFFF;
					margin-right: 12rpx;
				}
			}
		}

	}
</style>
